package com.intramirror.product.schedule.consumer;

import com.intramirror.product.service.FacilityService;
import com.intramirror.product.service.IPromotionService;
import com.intramirror.product.utils.ConvertUtil;
import com.intramirror.utils.transform.JsonTransformUtil;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created on 2018/4/17.
 * @author 123
 */
public class SnapshotConsumer implements IProductConsumer {
    private static final Logger LOGGER = LoggerFactory.getLogger(SnapshotConsumer.class);

    private FacilityService facilityService;

    SnapshotConsumer(FacilityService facilityService) {
        this.facilityService = facilityService;
    }

    @Override
    public void process(String message) {
        LOGGER.info("dispatch message:{}", message);
        List productList = JsonTransformUtil.readValue(message, List.class);
        if (productList.size() <= 0) {
            return;
        }

        IPromotionService promotionService = facilityService.getPromotionService();
        if (productList.size() == 1) {
            promotionService.refreshSnapshotForAddProduct(Long.valueOf(productList.get(0).toString()));
        } else {
            List<Long> products = new ArrayList<>();
            for (Object o : productList) {
                products.add(ConvertUtil.toLong(o));
            }
            promotionService.refreshBatchSnapshotForAddProduct(products);
        }
    }
}
